package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.util.Scanner;

public class StudentController {
    private Scanner sc = new Scanner(System.in);
    private StudentService studentService = new StudentService();

    public void studentInfo() {
        s:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            int a = sc.nextInt();
            switch (a) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudent();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    findAllStudent();
                    break;
                case 5:
                    System.out.println("感谢使用学生管理系统，再见");
                    break s;
                default:
                    System.out.println("您的输入有误，请重新输入");
            }
        }
    }

    private void updateStudent() {
        String id = inputById();
        Student s = input(id);
        studentService.updateStudent(id, s);
        System.out.println("修改成功");
    }

    private void deleteStudent() {
        studentService.deleteStudent(inputById());
        System.out.println("删除成功");
    }

    private String inputById() {
        System.out.println("请输入学号:");
        String id;
        while (true) {
            id = sc.next();
            if (studentService.isEmptyById(id)) {
                System.out.println("未查到该学生，请重新输入:");
            } else {
                break;
            }
        }
        return id;
    }

    private void findAllStudent() {
        Student[] stus = studentService.findAllStudent();
        if (stus == null) {
            System.out.println("查无信息, 请添加后重试");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            if (stus[i] != null) {
                System.out.println(stus[i].getId() + "\t\t" + stus[i].getName() + "\t\t" + stus[i].getAge() + "\t\t" + stus[i].getBirthday());
            }
        }

    }

    private void addStudent() {
        System.out.println("请输入学生的ID:");
        String id;
        while (true) {
            id = sc.next();
            if (!studentService.isEmptyById(id)) {
                System.out.println("学生ID已存在，请重新输入:");
            } else {
                break;
            }
        }
        Student student = input(id);
        if (studentService.addStudent(student)) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }

    }

    private Student input(String id) {
        System.out.println("请输入学生的姓名:");
        String name = sc.next();
        System.out.println("请输入学生的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学生的生日:");
        String birth = sc.next();
        Student s = new Student();
        s.setId(id);
        s.setName(name);
        s.setAge(age);
        s.setBirthday(birth);
        return s;
    }
}
